package org.example.leetCode;

import java.util.Stack;

/**
 * @author Administrator
 */
public class ValidateStackSequences {
    public static void main(String[] args) {
        int[] pushed = {1, 2, 4, 6, 8};
        int[] popped = {8, 6, 4, 2, 1};
        ValidateStackSequences validateStackSequences = new ValidateStackSequences();
        boolean b = validateStackSequences(pushed, popped);
        System.out.println(b);
    }

    public static boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack();
        int temp = 0;
        for (int i = 0; i < pushed.length; i++) {
            stack.push(pushed[i]);
            while (stack.peek() == popped[temp]) {
                stack.pop();
                temp++;
                if (stack.isEmpty()) {
                    break;
                }
            }
        }
        return stack.isEmpty();
    }
}
